/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tesam.gedeon.service.impl;

import com.tesam.gedeon.dao.AccountDao;
import com.tesam.gedeon.entity.Account;
import com.tesam.gedeon.service.AccountService;
import java.text.SimpleDateFormat;
import java.util.List;
import org.apache.commons.lang3.StringEscapeUtils;
import org.primefaces.json.JSONArray;
import org.primefaces.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author Sistema
 */
@Service
public class AccountServiceImpl implements AccountService {

    @Autowired
    AccountDao accountDao ;
    
    @Override
    public List<Account> getListByOwner(Account account){
        return accountDao.getListByOwner(account);
    }
    
    @Override
    public int getCountAll() {
        return accountDao.getCountAll();
    }

    @Override
    public int getCountBySearch(Account account) {
        return accountDao.getCountBySearch(account);
    }

    @Override
    public List<Account> getListByLimit(int startLimit, int endLimit, String dataToSort, String directionOfOrder) {
        return accountDao.getListByLimit(startLimit, endLimit, dataToSort, directionOfOrder);
    }

    @Override
    public List<Account> getListBySearch(int startLimit, int endLimit, String dataToSort, String directionOfOrder, Account account) {
        return accountDao.getListBySearch(startLimit, endLimit, dataToSort, directionOfOrder, account);
    }

    @Override
    public List<Account> getAccountById(Account account) {
        return accountDao.getAccountById(account);
    }

    @Override
    public int save(Account account) {
        return accountDao.save(account);
    }

    @Override
    public void update(Account account) {
        accountDao.update(account);
    }

    @Override
    public void delete(Account account) {
        accountDao.delete(account);
    }
    
    @Override
    public String getJsonDataFromArrayList(List<Account> listAccount, int quantityRecord){
        JSONArray array = null;
        JSONObject json = null;
        String deleteButton , groupButton  , viewButton , updateButton= "" ; 
        SimpleDateFormat format = null;

        try {

            json = new JSONObject();
            array = new JSONArray();

            if (listAccount.size() > 0) {


                format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                // Date date = sdf.parse("2004-07-24 09:45:52.189");
              
                for (Account account : listAccount) {
                    JSONArray arrayPut = new JSONArray();
 
                    viewButton = "<a href='view?id="+account.getId()+"&module="+account.getModuleId()+"' > ";
                    viewButton+= " <i class='icon-share'></i> View </a>"; 
                    
                    updateButton = "<a href='update?id="+account.getId()+"&module="+account.getModuleId()+"' class='green' >" ;
                    updateButton+= "<i class='icon-pencil'></i> Update</a>";
                    
                    deleteButton = "<a class='red' data-toggle='modal' ";
                    deleteButton+= "onclick=\"";     
                    deleteButton+="confirmDelete('"+account.getId()+"')";
                    deleteButton+="\" "; 
                    deleteButton+="href='#static' >";
                    deleteButton+="<i class='icon-trash'></i> Delete</a>"; 
                    
                    groupButton = "<div class='btn-group'>";
                    groupButton+= "<a class='btn mini purple' href='#' data-toggle='dropdown'>";
                    groupButton+= "Function&nbsp;&nbsp;";
                    groupButton+= "<i class='icon-arrow-down'></i>";
                    groupButton+= "</a>";
                    groupButton+= "<ul class='dropdown-menu'>";
                    groupButton+="<li>"+viewButton+"</li>";
                    groupButton+="<li>"+updateButton+"</li>";
                    groupButton+="<li>"+deleteButton+"</li>";                      
                    groupButton+= "</ul>";
                    groupButton+= "</div>"; 
                    
                    arrayPut.put(StringEscapeUtils.escapeHtml4(account.getName())); 
                    arrayPut.put(account.getAccountNo());
                    arrayPut.put(account.getPhone()); 
                    arrayPut.put(account.getEmail()); 
                    arrayPut.put(account.getWebsite()); 
                    arrayPut.put(account.getFax()); 
                    arrayPut.put(account.getFoundation()); 
                     
//                    arrayPut.put(account.getCountryBill().getName()); 
//                    arrayPut.put(account.getDepartmentBill().getName());
//                    arrayPut.put(account.getProvinceBill().getName());
//                    arrayPut.put(account.getDistrictBill().getName());
//                    arrayPut.put(account.getEmailBill());
//                    arrayPut.put(account.getZipcodeBill());
//                    arrayPut.put(account.getAddressBill());
//                    
//                    arrayPut.put(account.getCountryShipping().getName());
//                    arrayPut.put(account.getDepartmentShipping().getName());
//                    arrayPut.put(account.getProvinceShipping().getName());
//                    arrayPut.put(account.getDistrictShipping().getName());
//                    arrayPut.put(account.getEmailShipping());
//                    arrayPut.put(account.getZipcodeShipping());
//                    arrayPut.put(account.getAddressShipping());
                    
                    arrayPut.put(groupButton);
                    array.put(arrayPut);
                }
            }

            json.put("iTotalRecords", quantityRecord);
            json.put("iTotalDisplayRecords", quantityRecord);
            json.put("aaData", array);

            return json.toString();
        } catch (Exception e) {
            System.out.println("Exception getJsonDataFromArrayList:" + e.getMessage());
            return null;
        }
    }
    
}
